package com.multicampus.simplewiki.control.search;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.multicampus.simplewiki.ctrl.DocumentManager;
import com.multicampus.simplewiki.vo.SearchListVO;
import com.multicampus.simplewiki.vo.SearchResultVO;

public class SearchController implements Controller  {
	DocumentManager documentManager;
	
	public void setDocumentManager(DocumentManager documentManager) {
		this.documentManager = documentManager;
	}


	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String url = "/wikipage/searchView_page";
		int startPage;
		
		try{
			startPage = Integer.parseInt(request.getParameter("startPage"));
		}catch(Exception e){
			startPage = 1;
		}
		
		SearchListVO<SearchResultVO> results = documentManager.searchDocument(request.getParameter("queryString"), startPage, 10);

		return new ModelAndView(url, "results", results);
	}
}
